package com.lvmama.rhino.web.comment.predict.controller;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageInfo;
import com.lvmama.rhino.pojo.comment.Enum.EnumForCategory;
import com.lvmama.rhino.pojo.comment.entity.vo.FavorRatePredictResultVO;
import com.lvmama.rhino.pojo.comment.entity.vo.ProPredictCalcResultVO;
import com.lvmama.rhino.pojo.comment.request.PredictFavorRateRequest;
import com.lvmama.rhino.pojo.comment.request.PredictSalesNumRequest;
import com.lvmama.rhino.service.comment.PredictService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * 产品预测
 */

@Controller
@RequestMapping("predict")
public class PredictController {

    private static Logger log = Logger.getLogger(PredictController.class);

    @Autowired
    private PredictService predictService;

    @RequestMapping("salesNumPredict")
    public String salesNumPredictPage(ModelMap model) {
        model.put("page", new PageInfo<ProPredictCalcResultVO>());
        // 品类
        EnumForCategory[] categories = EnumForCategory.getAllCategory();
        model.put("categories", categories);
        return "application/predict/salesNumPredict";
    }

    @RequestMapping("favorRatePredict")
    public String favorRatePredict(ModelMap model) {
        model.put("page", new PageInfo<FavorRatePredictResultVO>());
        // 品类
        EnumForCategory[] categories = EnumForCategory.getAllCategory();
        model.put("categories", categories);
        return "application/predict/favorRatePredict";
    }

    /**
     * 1.产品需求量预测
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "getPredictSalesNum", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getPredictSalesNum(PredictSalesNumRequest request) {

        log.info("predict getPredictSalesNum start");
        Map<String, Object> result = new HashMap<>();
        try {
            if (null != request) {
                log.info("predict getPredictSalesNum request:" + request.toString());
            }
            PageInfo<ProPredictCalcResultVO> proPredictList = predictService.getPredictSalesNum(request);
            result.put("page", proPredictList);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("predict getPredictSalesNum error:" + e.getMessage(), e);
        }
        log.info("predict getPredictSalesNum end");
        return result;
    }

    /**
     * 1-1. 产品需求量预测趋势图
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "getPredictSalesNumChart", method = RequestMethod.POST)
    @ResponseBody
    public JSONArray getPredictSalesNumChart(PredictSalesNumRequest request) {
        log.info("predict getPredictSalesNumChart start");
        JSONArray jsonArray = new JSONArray();
        try {
            jsonArray = predictService.getPredictSalesNumChart(request);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("predict getPredictSalesNumChart error:" + e.getMessage(), e);
        }
        return jsonArray;
    }

    /**
     * 2. 产品好评率预测
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "getPredictFavorRate", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getPredictFavorRate(PredictFavorRateRequest request) {
        log.info("predict getPredictFavorRate start");
        Map<String, Object> result = new HashMap<>();
        try {
            if (null != request) {
                log.info("predict getPredictFavorRate request:" + request.toString());
            }
            PageInfo<FavorRatePredictResultVO> proPredictList = predictService.getPredictFavorRate(request);
            result.put("page", proPredictList);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("predict getPredictFavorRate error:" + e.getMessage(), e);
        }
        log.info("predict getPredictFavorRate end");
        return result;
    }

    /**
     * 2-2. 产品好评率预测趋势图
     * @param request
     * @return
     */
    @RequestMapping(value = "getPredictFavorRateChart", method = RequestMethod.POST)
    @ResponseBody
    public JSONArray getPredictFavorRateChart(PredictFavorRateRequest request) {
        log.info("predict getPredictFavorRateChart start");
        JSONArray jsonArray = new JSONArray();
        try {
            jsonArray = predictService.getPredictFavorRateChart(request);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("predict getPredictFavorRateChart error:" + e.getMessage(), e);
        }
        return jsonArray;
    }

}
